//
//  HLTgonghuifanliViewController.m
//  huiliantong
//
//  Created by hlt on 15/10/23.
//  Copyright © 2015年 hlt. All rights reserved.
//

#import "HLTgonghuifanliViewController.h"
#import "Fanlimodel.h"
#import "HLTgonghuifanliTableViewCell.h"
#import "MJExtension.h"
#import "MJRefresh.h"
@interface HLTgonghuifanliViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    NSMutableArray *_recommendArray;
    NSInteger _KindID;//分类查询ID，默认-1
    NSInteger _offset;
    
}


@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (weak, nonatomic) IBOutlet UILabel *zongjine;

@property (weak, nonatomic) IBOutlet UILabel *fanlijine;


@end

@implementation HLTgonghuifanliViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.delegate=self;
    self.tableView.dataSource=self;
    //设置返回的按钮的字体的颜色的
    self.navigationController.navigationBar.tintColor=[UIColor whiteColor];
    //设置导航的背景颜色的
    self.navigationController.navigationBar.barTintColor = navigationBarColor;
    _recommendArray = [[NSMutableArray alloc] init];   //商铺数据
    self.navigationController.navigationBarHidden = YES;
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.extendedLayoutIncludesOpaqueBars = NO;
    self.modalPresentationCapturesStatusBarAppearance = NO;
    
    
    [self initViews];
    
    
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(void)initViews{
    
    
    
    
    //设置没有下划线
//    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self setUpTableView];
    
    
    //去掉多余分割线的方法
    UIView *view = [UIView new];
    
    view.backgroundColor = [UIColor clearColor];
    
    [self.tableView setTableFooterView:view];
}




-(void)setUpTableView{
    //添加下拉的动画图片
    //设置下拉刷新回调
    [self.tableView addGifHeaderWithRefreshingTarget:self refreshingAction:@selector(getFirstPageData)];
    
    //设置普通状态的动画图片
//    NSMutableArray *idleImages = [NSMutableArray array];
//    for (NSUInteger i = 1; i<=60; ++i) {
//        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"dropdown_anim__000%zd",i]];
//        [idleImages addObject:image];
//    }
//    [self.tableView.gifHeader setImages:idleImages forState:MJRefreshHeaderStateIdle];
//    
//    //设置即将刷新状态的动画图片
//    NSMutableArray *refreshingImages = [NSMutableArray array];
//    for (NSInteger i = 1; i<=3; i++) {
//        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"dropdown_loading_0%zd",i]];
//        [refreshingImages addObject:image];
//    }
//    [self.tableView.gifHeader setImages:refreshingImages forState:MJRefreshHeaderStatePulling];
//    
//    //设置正在刷新是的动画图片
//    [self.tableView.gifHeader setImages:refreshingImages forState:MJRefreshHeaderStateRefreshing];
    
    //马上进入刷新状态
    [self.tableView.gifHeader beginRefreshing];
    
    
    //上拉刷新
    [self.tableView addGifFooterWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    
    //隐藏状态文字
    //    self.tableView.footer.stateHidden = YES;
    //设置正在刷新的动画
//    self.tableView.gifFooter.refreshingImages = refreshingImages;
    
    
    
    
}


-(void)getFirstPageData{
    _offset = 0;
    [self refreshData];
}

-(void)refreshData{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [self getMerchantList];
    });
}

-(void)loadMoreData{
    NSLog(@"2222  offset:%zd",_offset);
    _offset = _offset + 10;
    NSLog(@"3333  offset:%zd",_offset);
    [self refreshData];
}


//获取商家列表
-(void)getMerchantList{
    //    AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    
    
    
    Account *acc=[[AccountTool sharedAccountTool]currentAccount];
    
    
    NSInteger page1=_offset/10;
    ////查询返利列表(&card_num=8800031104000000198)
    NSString *url=[fanlilist stringByAppendingFormat:@"&card_num=%@&page=%ld",acc.cardNo,(long)page1];
    //    NSString *url=@"http://www.hltgz.com/wx/mobile/index.php?m=default&c=appinterface&a=recommend&page=0";
    NSLog(@"%@",url);
    
    
    [HLTNetWorkingtool getdic:url success:^(NSDictionary *ary) {
        
        
        if (_offset<10) {
            [_recommendArray removeAllObjects];
        }
        NSArray *fanliary=ary[@"data"];
        
        for (int i = 0; i < fanliary.count; i++) {
            Fanlimodel *recommend = [Fanlimodel objectWithKeyValues:fanliary[i]];
            [_recommendArray addObject:recommend];
        }
        [self.tableView reloadData];
        //刷新指定的section,显示推荐商铺的部分
        //        NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:0];
        //        [self.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];
        [self.tableView.header endRefreshing];
        [self.tableView.footer endRefreshing];
        
        self.fanlijine.text=[NSString stringWithFormat:@"%@元",ary[@"fanli"]];
        self.zongjine.text=[NSString stringWithFormat:@"%@元",ary[@"xiaofei"]];
        
        
    } faliure:^(NSString *error) {
        
        
        
    }];
    
    
    
    
    
    
    
    
}





#pragma mark - UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _recommendArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 70;
}




-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    
    static NSString *cellIndentifier = @"HLTgonghuifanliTableViewCell";
    HLTgonghuifanliTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
    if(cell == nil)
    {
        cell= [[[NSBundle mainBundle]loadNibNamed:@"HLTgonghuifanliTableViewCell" owner:nil options:nil] firstObject];
    }
    
    Fanlimodel *fanlidata=_recommendArray[indexPath.row];
    
    
    cell.date.text=[NSString stringWithFormat:@"%@ %@",fanlidata.TranDate,fanlidata.TranTime];
    
    cell.xiaofeifangshi.text=fanlidata.Source;
    cell.jine.text=fanlidata.TranAmt1;
    cell.fanli.text=fanlidata.RebateAmt;
    //设置点击没有效果
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
    
    
    
}


//将要出现的时候
- (void)viewWillAppear:(BOOL)animated
{
    //打开导航
    self.navigationController.navigationBarHidden=NO;
    
}


@end

